//
//  UICollectionViewTestVC.m
//  TestPrj
//
//  Created by 朱铅峰 on 2019/11/9.
//  Copyright © 2019 Xing. All rights reserved.
//

#import "WaterFallByCollectionViewVC.h"
#import <Masonry.h>
#import "WaterFallCollectionViewLayout.h"
#import "XCollectionViewLayout.h"
#import "XCWaterCollectionViewLayout.h"

@interface WaterFallByCollectionViewVC ()<UICollectionViewDataSource,UICollectionViewDelegate,WaterFallCollectionViewLayoutDelegate,XCWaterCollectionViewLayoutDelegate>

@property(nonatomic,assign)NSInteger curType;
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)WaterFallCollectionViewLayout *WFlayout;
@property(nonatomic,strong)XCollectionViewLayout *xCLayout;
@property(nonatomic,strong)XCWaterCollectionViewLayout *xcwLayout;

@end

@implementation WaterFallByCollectionViewVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    UIButton *styleBtn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    styleBtn1.tag = 1;
    [styleBtn1 addTarget:self action:@selector(navItemAction:) forControlEvents:UIControlEventTouchUpInside];
    [styleBtn1 setTitle:@"样式1" forState:UIControlStateNormal];
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithCustomView:styleBtn1];
    
    UIButton *styleBtn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    styleBtn2.tag = 2;
    [styleBtn2 addTarget:self action:@selector(navItemAction:) forControlEvents:UIControlEventTouchUpInside];
    [styleBtn2 setTitle:@"样式2" forState:UIControlStateNormal];
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithCustomView:styleBtn2];
    
    UIButton *styleBtn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    styleBtn3.tag = 3;
    [styleBtn3 addTarget:self action:@selector(navItemAction:) forControlEvents:UIControlEventTouchUpInside];
    [styleBtn3 setTitle:@"样式3" forState:UIControlStateNormal];
    UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithCustomView:styleBtn3];
    
    self.navigationItem.rightBarButtonItems = @[item1,item2,item3];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)navItemAction:(UIButton *)sender {
    
    self.curType = sender.tag;
    
    [self.view removeAllSubViews];
    
    switch (self.curType) {
        case 1:
        {
            _WFlayout = [WaterFallCollectionViewLayout new];
            _WFlayout.delegate = self;
            
            _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_WFlayout];
            _collectionView.dataSource = self;
            _collectionView.delegate = self;
            [_collectionView registerClass:[WaterFallCollectionCell class] forCellWithReuseIdentifier:@"cell"];
            [self.view addSubview:_collectionView];
            
            [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(UIEdgeInsetsZero);
            }];
            break;
        }
        case 2:
        {
            _xCLayout = [XCollectionViewLayout new];
            _xCLayout.transformStyle = XCollectionViewLayout_LinerTransform;
            _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_xCLayout];
            _collectionView.dataSource = self;
            _collectionView.delegate = self;
            [_collectionView registerClass:[WaterFallCollectionCell class] forCellWithReuseIdentifier:@"cell"];
            [self.view addSubview:_collectionView];
            
            [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(UIEdgeInsetsZero);
            }];
            
            break;
        }
        case 3:
        {
            _xcwLayout = [XCWaterCollectionViewLayout new];
            _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_xcwLayout];
            _collectionView.dataSource = self;
            _collectionView.delegate = self;
            [_collectionView registerClass:[WaterFallCollectionCell class] forCellWithReuseIdentifier:@"cell"];
            [self.view addSubview:_collectionView];
            
            [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(UIEdgeInsetsZero);
            }];
            break;
        }
        default:
            break;
    }
    
}

- (void)orientChange:(NSNotification *)notification {
    
    [_WFlayout invalidateLayout];
    [_xCLayout invalidateLayout];
    [_xcwLayout invalidateLayout];
    
    [_collectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 50;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    WaterFallCollectionCell *cell = [_collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    float r = (arc4random() % 255)/255.0;
    float g = (arc4random() % 255)/255.0;
    float b = (arc4random() % 255)/255.0;
    
    cell.backgroundColor = [UIColor colorWithRed:r green:g blue:b alpha:1.0];
    cell.titleLabel.text = [NSString stringWithFormat:@"%d",(int)indexPath.row];
    
    return cell;
}

#pragma mark - UICollectionViewFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(self.view.width-80, self.view.height-200);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    return UIEdgeInsetsZero;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    
    return 10;
}

#pragma mark - XCWaterCollectionViewLayoutDelegate

/** 列数 */
- (NSInteger)itemColumns {
    
    return 2;
}

/** item与item之间 水平间距 */
- (CGFloat)itemSpaceAtSectioin:(NSInteger)section {
    
    return 10;
}

/** item与item之间 垂直间距 */
- (CGFloat)itemLineSpaceAtSection:(NSInteger)section {
    
    return 10;
}

- (UIEdgeInsets)sectionInsetsAtSection:(NSInteger)section {
    
    return UIEdgeInsetsZero;
}

/** item 高度，XCWaterCollectionViewScrollDirectionVertical模式下返回item宽度 */
- (CGFloat)itemHeightAtIndexPath:(NSIndexPath *)indexPath {
    
    return 120;
}

/** header view */
- (CGSize)headerViewSizeAtSection:(NSInteger)section {
    
    return CGSizeZero;
}

/** footer view */
- (CGSize)footerViewSizeAtSection:(NSInteger)section {
    
    return CGSizeZero;
}

#pragma mark - WaterFallCollectionViewLayout

/**
 * 每个item的高度
 */
- (CGFloat)waterFallLayout:(WaterFallCollectionViewLayout *)waterFallLayout heightForItemAtIndexPath:(NSUInteger)indexPath itemWidth:(CGFloat)itemWidth {
    
    CGFloat height = ((arc4random() % 7)+2)*10;
    
    return height;
}

/**
 * 有多少列
 */
- (NSUInteger)columnCountInWaterFallLayout:(WaterFallCollectionViewLayout *)waterFallLayout {
    
    return 3;
}

/**
 * 每列之间的间距
 */
- (CGFloat)columnMarginInWaterFallLayout:(WaterFallCollectionViewLayout *)waterFallLayout {
    
    return 10;
}

/**
 * 每行之间的间距
 */
- (CGFloat)rowMarginInWaterFallLayout:(WaterFallCollectionViewLayout *)waterFallLayout {
    
    return 10;
}

/**
 * 每个item的内边距
 */
- (UIEdgeInsets)edgeInsetdInWaterFallLayout:(WaterFallCollectionViewLayout *)waterFallLayout {
    
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
//
//    CGFloat width = (CGRectGetWidth(self.view.frame)-30)/2;
//
//    CGFloat height = ((arc4random() % 7)+2)*10;
//
//    return CGSizeMake(width, height);
//}
//
//- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
//
//    return UIEdgeInsetsMake(10, 10, 10, 10);
//}
//
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
//
//    return 10;
//}
//
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
//
//    return 10;
//}

@end


#pragma mark -
#pragma mark CustomCell


@implementation WaterFallCollectionCell

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        [self addViews];
    }
    return self;
}

- (void)addViews {
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.font = [UIFont systemFontOfSize:20];
    _titleLabel.textColor = [UIColor blackColor];
    [self addSubview:_titleLabel];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
}

@end
